import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class 非常男女计划 {
    // 快读快写
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    // 开始创建足够大的数组
    public static int MAXN = 100001;
    public static int[] arr = new int[MAXN];
    // 哈希表来存储数据
    public static Map<Integer,Integer> map = new HashMap<>();
    public static int n;
    public static void main(String[] args) throws IOException {
        n = in.nextInt();
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        out.println(compute());
        out.close();
    }

    private static int compute() {
        int ans = 0; // 返回值
        map.put(0,-1); // 初始化
        for (int i = 0,sum = 0; i < n; i++) {
            sum += arr[i] == 0 ? -1 : 1; // 转换操作, 将 0 转化成 -1
            if (sum == 0) {
                // 如果刚好为 0, 直接返回 i + 1, 其实 sum 也可以, 我们进行初始化了
                ans = i + 1;
            } else {
                // 如果存在 sum 的话, 就取一下, 更新一下最大值
                if (map.containsKey(sum)) {
                    ans = Math.max(ans,i - map.get(sum));
                } else {
                    // 不存在就进行更新
                    map.put(sum,i);
                }
            }
        }
        return ans;
    }


    public static class Read {
        StringTokenizer st = new StringTokenizer("");
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String next() throws IOException {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(bf.readLine());
            }
            return st.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
    }
}
